/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.region.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.page.PagerResult;
import net.siufung.core.result.Result;
import net.siufung.core.utils.StringPool;
import net.siufung.market.base.region.api.IRegionApi;
import net.siufung.market.base.region.api.vo.RegionApiDto;
import net.siufung.market.base.region.controller.assembler.RegionAssembler;
import net.siufung.market.base.region.controller.model.dto.RegionTreeDto;
import net.siufung.market.base.region.controller.model.dto.page.RegionPageDto;
import net.siufung.market.base.region.mapper.IRegionMapper;
import net.siufung.market.base.region.mapper.entity.Region;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 行政区域信息 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-07 14:18:39
 */
@Api(tags = "行政区域信息")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/data/region")
public class RegionController {

    private final IRegionMapper regionMapper;
    private final IRegionApi regionApi;

    /**
     * 通过主键获取 行政区域信息
     * @param id 主键
     * @return Result<RegionApiDto>
     * */
    @ApiOperation(value = "通过Id获取行政区域信息信息")
    @GetMapping(value = "/id/{id}")
    public Result<RegionApiDto> getById(
            @PathVariable("id") Integer id) {
        return Result.ok(
            RegionAssembler.from(regionMapper.selectById(id)));
    }

    /**
     * 获取 应用信息分页
     * @param regionPageDto 属性
     * @return 查询分页结果
     * */
    @ApiOperation(value = "获取应用信息分页")
    @PostMapping(value="/page")
    public Result<PagerResult<RegionApiDto>> getPage(
            @RequestBody RegionPageDto regionPageDto) {
        regionPageDto.validated();
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(!Strings.isNullOrEmpty(regionPageDto.getCode()) ,
                Region::getCode, regionPageDto.getCode());
        wrapper.lambda().orderByAsc(Region::getId);
        IPage<Region> regionPage = regionMapper.selectPage(
                new Page<>(regionPageDto.getPageNo(), regionPageDto.getPageSize()), wrapper);
        PagerResult<RegionApiDto> regionVoPager = new PagerResult<>(
                regionPage.getCurrent(), regionPage.getSize(), regionPage.getTotal());
        regionVoPager.setRecords(
                RegionAssembler.fromList(regionPage.getRecords()));
        return Result.ok(regionVoPager);
    }

    /**
     * 获取省信息列表
     * @return Result<List<RegionApiDto>>
     */
    @ApiOperation(value = "获取省信息列表")
    @GetMapping(value = "/getProvinceList")
    public Result<List<RegionApiDto>> getProvinceList() {
        return Result.ok(regionApi.getProvinceList());
    }

    /**
     * 获取市信息列表
     * @param  provinceId 省id
     * @return Result<List<RegionApiDto>>
     */
    @ApiOperation(value = "获取市信息列表")
    @GetMapping(value = "/getCityList")
    public Result<List<RegionApiDto>> getCityList(
            @RequestParam(value = "provinceId", required = false) Integer provinceId) {
        if(Objects.isNull(provinceId)){
            return Result.ok(regionApi.getCityList());
        }
        return Result.ok(regionApi.getCityList(provinceId));
    }

    /**
     * 获取区信息列表
     * @param  cityId 市id
     * @return Result<List<RegionApiDto>>
     */
    @ApiOperation(value = "获取区信息列表")
    @GetMapping(value = "/getAreaList")
    public Result<List<RegionApiDto>> getAreaList(
            @RequestParam(value = "cityId", required = false) Integer cityId) {
        if(Objects.isNull(cityId)){
            return Result.ok(regionApi.getAreaList());
        }
        return Result.ok(regionApi.getAreaList(cityId));
    }

    /**
     * 获得区域树
     * @param parentId 父级Id
     * @return  Result<List<Tree<Integer>>>
     */
    @ApiOperation(value = "获得区域树", notes = "")
    @GetMapping(value = "/getRegionTree")
    public Result<List<Tree<Integer>>> getRegionTree(
            @RequestParam(value = "parentId", required = false) Integer parentId) {
        if (ObjectUtil.isNull(parentId)) {
            parentId = Integer.valueOf(StringPool.ZERO);
        }
        return Result.ok(regionMapper.getRegionTree(parentId));
    }

    /**
     * 获取树表格
     * @param regionTreeDto 树查询参数
     * @return Result<List<Tree<String>>>
     */
    @ApiOperation(value = "获取树表格")
    @PostMapping(value = "/getTreeTable")
    public Result<List<Tree<Integer>>> getTreeTable(
            @RequestBody RegionTreeDto regionTreeDto) {
        return Result.ok(regionMapper.getTreeTable(regionTreeDto));
    }



}